package com.glory.eapequipmentmonitor.fragment;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.eapequipmentmonitor.R;
import com.glory.eapequipmentmonitor.activity.PackPackageActivity;
import com.glory.eapequipmentmonitor.adapter.PackListAdapter;
import com.glory.eapequipmentmonitor.bean.PackBean;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseFragment;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AlreadyDoneFragment extends FreeUI_BaseFragment {

    public PackListAdapter packListAdapter;
    @BindView(R.id.rvThingList)
    RecyclerView rvThingList;
    @BindView(R.id.llMain)
    FrameLayout llMain;
    @BindView(R.id.checkAll)
    CheckBox checkAll;

    public AlreadyDoneFragment() {
        // Required empty public constructor
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_already_done;
    }

    @Override
    protected void initView() {
        rvThingList.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvThingList.setItemAnimator(new DefaultItemAnimator());
        rvThingList.addItemDecoration(new DividerItemDecoration(getActivity(), 1));

        packListAdapter = new PackListAdapter(null);
        packListAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        packListAdapter.isFirstOnly(true);
        rvThingList.setAdapter(packListAdapter);
    }

    @Override
    protected void initData() {
        checkAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                packListAdapter.setAll(isChecked);
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return null;
    }


    @OnClick({R.id.tvPack, R.id.tvPrint})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvPack:
                final List<PackBean> selectList = new ArrayList<>();
                for (PackBean packBean : packListAdapter.getData()) {
                    if (packBean.isSelect()) {
                        selectList.add(packBean);
                    }
                }
                if (selectList.size() == 0) {
                    showToast("请选择要拆包的批次", false);
                    return;
                }
                packListAdapter.getData().removeAll(selectList);
                packListAdapter.clearPos();
                packListAdapter.notifyDataSetChanged();
                showToast("拆包成功", true);
                break;
            case R.id.tvPrint:
                final List<PackBean> selectList2 = new ArrayList<>();
                for (PackBean packBean : packListAdapter.getData()) {
                    if (packBean.isSelect()) {
                        selectList2.add(packBean);
                    }
                }
                if (selectList2.size() == 0) {
                    showToast("请选择要打印的批次", false);
                    return;
                }
                showToast("打印成功", true);
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            ((PackPackageActivity) getActivity()).etLotId.setEnabled(false);
        }
    }
}
